<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$return['post']=array();
$return['paytypes']=array(
		'credit'=>'余额支付',
		'wechat'=>'微信支付',
		'alipay'=>'支付宝支付',
		'yinlian'=>'银联支付',
		'hdfk'=>'货到付款',
);
if($_RQ['op']=='main'){
	$return['params']=array('status'=>intval($_RQ['status']));
	exi($return);
}
/*******op*******/
set_html('title','配送情况');
if($_RQ['op']=='list'){
	$params=array('mid'=>$_SESSION['uid'],'merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'sendtype'=>1,'deleted'=>0,'userdeleted'=>0);
	$params['status']=intval($_RQ['status']);
	if($_RQ['today']){
		$params['createtime >']=strtotime(date('ymd'));
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$order_list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,array(),'','createtime desc');
	if($order_list){
		foreach($order_list as &$item){
			$item['statustitle']=$this->order_status[$item['status']];
			$user=pdo_get('jy_weishop_member',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
			if($item['merchid']){
				$store=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']));
				$store['title']=$store['merchname'];
			}else{
				$store=pdo_get('jy_weishop_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']));	
			}
			$store['pcd']=json_decode($store['pcd'],true);
			$item['store']=$store;
			$item['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id']));
			$item['user_name']=$user['name'];
			$item['user_mobile']=$user['mobile'];
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$item['paytime']=$item['paytime']?date('Y-m-d H:i:s', $item['paytime']):'';
			$store_times=pdo_getcolumn('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'onlinetime');
			$store_times=json_decode($store_times,true);
			$item['serve_time']=$store_times[$item['timeid']];
			//用户地址
			$item['address']=json_decode($item['address'],true);
			if($item['address'] && ($item['address']['name'] || $item['address']['realname'])){
				$address=$item['address'];
			}else{
				$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['addressid']));
			}
			if(!is_array($address['pcd'])){
				$address['pcd']=json_decode($address['pcd'],true);
			}
			$item['order_out']=$address;
			$item['goodsnum']=0;
			if($item['goods']){
				foreach($item['goods'] as &$g){
					$goods=pdo_get('jy_weishop_goods',array('id'=>$g['goodsid']));
					$g['unit']=$goods['unit'];
					$g['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$g['goodsid']),'weight');
					if($g['optionid']){
						$specs = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$g['goodsid'],'id'=>$g['optionid']),'specs');
						$g['price']=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$g['goodsid'],'id'=>$g['optionid']),'price');
						$specs = explode("_", $specs);
						$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
						if($specsall){
							$optiontitle='';
							foreach($specsall as $k=>&$sp){
								if($k==0){
									$optiontitle .=$sp['title'];
								}else{
									$optiontitle .='，'.$sp['title'];
								}
							}
						}
						$g['optiontitle']= $optiontitle;
					}
					$g['totalprice']=$g['price']?:0;
					$g['goods_title']=$goods['title'];
					$item['goods_num'] +=$g['goods_num'];
					$g['image']=$goods['image'];
					if($item['goodsnum']<$g['goodsnum']){
						$item['goodsnum']=$g['goodsnum'];
					}
				}
			}
			$item['issendnum']=$this->model('order')->get_goodsnum($item['id'],'',true);
			$item['lastsendnum']=$item['goodsnum']-$item['issendnum'];
			$selfinfo=pdo_get('jy_weishop_order_self',array('orderid'=>$item['id']));
			$selfinfo['selftime']=$selfinfo['selftime']?date('Y-m-d H:i:s',$selfinfo['selftime']):'尽快配送';
			$selfinfo['selftime']=$selfinfo['out_time']?:$selfinfo['selftime'];
			$selfinfo['number2']=substr($selfinfo['number'],-4);
			$item['selfinfo']=$selfinfo;
			$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
			$peiset=$this->pdo_get('peiset',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$item['id']));
			$days=str_array($peiset['days']);
			$item['times']=str_array($peiset['times']);
			$daytime= mktime(0,0,0,date('m'),date('d'),date('Y'));
			$item['nowlog']=pdo_get('jy_peiorder_sendlog',array('status'=>0,'daytime >'=>$daytime,'order_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'','daytime asc');
			$item['nowlog']['statustitle']=$this->send_status[$item['nowlog']['status']];
			$item['nowlog']['statustitle']=$item['nowlog']['statustitle']?:'未发起';
			$item['peiday']=$item['nowlog']['day'];
			$item['peisetgoods']=json_decode($peiset['goods'],true);
			if($item['peiday']){
				$item['tips']='最近配送时间：'.$item['peiday'];
				$item['tipgoods']='配送的商品类型和数量：';
			}elseif($item['status']==1 || $item['status']==2){
				$item['tips']='您没有预约，未来配送的时间！';
				$item['tipgoods']='您没有设置配送的商品类型和数量！';
			}
			$item['peisong_fee']=$this->model('model')->get_peifee($item,$_SESSION['uid']);
			if($item['peisong_id']){
				$item['peimember']=pdo_get('jy_peiorder_member',array('mid'=>$item['peisong_id'],'uniacid'=>$_SESSION['uniacid']),array('title','mobile','level_id'));
				$item['peimember']['level']=pdo_getcolumn('jy_peiorder_level',array('id'=>$item['peimember']['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
				$item['peimember']['level']=$item['peimember']['level']?:'普通等级';
			}
		}
	}
	$return['list']=$order_list;
	exi($return);
}

//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	$user=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$store=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['storeid']));	
	$store_times=json_decode($store['serve_time'],true);
	$order['serve_time']=$store_times[$order['timeid']];
	//自定义字段数据
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['store']=$store;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='receive'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_receive($order_id);
	exi('恭喜你完成一单！感谢你的努力！');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_weishop_order',array('status'=>'1'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('取消订单成功');
}
//转单
if($_RQ['op']=='change'){
	$post=get_postdata();
	$order_id=intval($post['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->up_sendlog($order_id,'',3);
	if($post['paisong_id']){
		pdo_update('jy_weishop_order',array('peisong_id'=>$post['paisong_id'],'send_status'=>13),array('id'=>$order_id));
		$this->model('order')->create_sendlog($order_id,$post['paisong_id']);
	}else{
		pdo_update('jy_weishop_order',array('peisong_id'=>0,'status'=>1,'send_status'=>12),array('id'=>$order_id));
	}
	exi('转单成功！');
}
//员工确认接单
if($_RQ['op']=='jieorder'){
	$order_id=intval($_RQ['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->order_checkget($order_id);
	exi('接单成功！');
}
//报备
if($_RQ['op']=='baobei'){
	$post=get_postdata();
	$order_id=intval($post['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	if($post['baobeitype']==3){
		$msg=$this->baobei_types3[$post['othertype']];
	}else{
		$msg=$this->baobei_types[$post['baobeitype']];
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
	model('jy_weishop/order')->add_baobei($order_id,$msg,2,'',$order['merchid'],'jy_merch');
	exi('报备成功！','','jy_peiorder/worker_order/list/');
}